<?php
namespace app\index\controller;

use app\common\entity\User;
use app\common\shop\AddressModel;
use app\common\shop\OrderModel;
use think\Db;
use think\Request;
use think\Controller;
class Shopaddress extends Base {

    /**
     * [默认发货地址]
     * @param
     * @return
     */
    public function getAddressDefault( Request $request ){
        $uid = $this->userId;
        $address = AddressModel::getDefault($uid);
        return json(['code' => 0, 'msg' => '请求成功','info'=>$address]);
    }

    /**
     * [添加/修改 地址]
     * @param
     * @return
     */
    public function addAddress( Request $request ){
        $uid = $this->userId;
        $result = $this->validate($request->post(), 'app\index\validate\ShopAddAddress');
        if ($result !== true) {
            return json(['code' => 1, 'msg' => $result]);
        }
        $data = [
            'uid'=>$uid,
            'name'=>$request->post('name'),
            'mobile'=>$request->post('mobile'),
            'province'=>$request->post('province'),
            'city'=>$request->post('city'),
            'area'=>$request->post('area'),
            'address'=>$request->post('address'),
            'isdefault'=>$request->post('isdefault'),
        ];

        $addressid = $request->post('id');// 地址id
        $time=date('H.i.s',time());
        //file_put_contents(dirname(__FILE__).'/addaddress'.$time,json_encode($addressid));

        if($addressid){
            AddressModel::edit($data,$addressid);
        }else{
            AddressModel::add($data);
        }
        return json(['code'=>0,'msg'=>'完成']);
    }
    /**
     * [获取地址详细列表]
     * @return
     */
    public function getAddressList(Request $request){
        $uid = $this->userId;
        $page = $request->post('page',1);
        $limit = $request->post('limit',100);
        $list = AddressModel::where('uid',$uid)
            ->page($page,$limit)
            ->order('isdefault desc,updatetime desc')
            ->select();
        $time=date('H.i.s',time());
        //file_put_contents(dirname(__FILE__).'/list'.$time,json_encode($list));

        return json(['code'=>0,'msg'=>'请求成功','info'=>$list]);
    }
    /**
     * [获取地址详细信息]
     * @return
     */
    public function getAddressDetail(Request $request){
        $uid = $this->userId;
        $id = $request->post('id');
        $address = AddressModel::getInfo($id,$uid);
        if(empty($address)) return json(['code'=>1,'msg'=>'地址不存在']);
        return json(['code'=>0,'msg'=>'请求成功','info'=>$address]);
    }
    /**
     * [设置默认地址]
     * @return
     */
    public function setDefault(Request $request){
        $uid = $this->userId;
        $id = $request->post('id');
        $address = AddressModel::getInfo($id,$uid);
        if(empty($address)) return json(['code'=>1,'msg'=>'地址不存在']);
        $isdefault = $request->post('isdefault');
        $address->setDefault($address['id'],$isdefault);//设置默认地址
        return json(['code'=>0,'msg'=>'设置成功']);
    }

    /**
     * [删除地址]
     * @return
     */
    public function deladdress(Request $request){
        $uid = $this->userId;
        $id = $request->post('id');
        $address = AddressModel::getInfo($id,$uid);
        if(empty($address)) return json(['code'=>1,'msg'=>'地址不存在']);
        $order = OrderModel::where('uid',$uid)->where('status','<',2)->where('addressid',$id)->find();
        if(!empty($order)) return json(['code'=>0,'msg'=>'存在订单未发货不能删除']);
        $address->delete();
        return json(['code'=>0,'msg'=>'删除完成']);
    }
}